//
//  HDImageMenuView.m
//  SocietySale
//
//  Created by yumingwei on 2016/11/6.
//  Copyright © 2016年 Mike. All rights reserved.
//

#import "HDImageMenuView.h"

@implementation HDImageMenuView

- (id)initWithFrame:(CGRect)frame titleArray:(NSArray *)titleArray
{
    self = [super initWithFrame:frame];
    if (self) {
        self.titleArray = titleArray;
        self.backgroundColor = [UIColor whiteColor];
    }
    return self;
}

- (void)setTitleArray:(NSArray *)titleArray
{
    _titleArray = titleArray;
    [self loadBtns];
}

//- (void)setFont:(UIFont *)font
//{
//    _font = font;
//    for (int i=0; i<_titleArray.count; i++) {
//        UIButton *btn = (UIButton *)[self viewWithTag:100+i];
//        btn.titleLabel.font = font;
//    }
//}

- (void)setNormalColor:(UIColor *)normalColor
{
    _normalColor = normalColor;
    for (int i = 0 ; i <_titleArray.count; i++) {
        GXButton *btn = (GXButton *)[self viewWithTag:100+i];
        [btn setTitleColor:normalColor forState:UIControlStateNormal];
    }
}

- (void)setSelectedColor:(UIColor *)selectedColor
{
    _selectedColor = selectedColor;
    for (int i = 0 ; i <_titleArray.count; i++) {
        GXButton *btn = (GXButton *)[self viewWithTag:100+i];
        [btn setTitleColor:selectedColor forState:UIControlStateSelected];
    }
}

//是否隐藏灰色分割线
- (void)setIsHiddenBottomLine:(BOOL)isHiddenBottomLine
{
    _isHiddenBottomLine = isHiddenBottomLine;
    if (self.isHiddenBottomLine == NO) {
        UIImageView *menuLine = [[UIImageView alloc] initWithFrame:CGRectMake(0, self.height-0.5, self.width, 0.5)];
        menuLine.image = [UIImage imageNamed:@"dot"];
        [self addSubview:menuLine];
    }
}


- (void)loadBtns
{
    for (int i=0; i<_titleArray.count; i++) {
        GXButton *btn = [GXButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(i*self.width/_titleArray.count, 0, self.width/_titleArray.count, self.height);
        if (self.font == nil)
        {
            btn.titleLabel.font = HeitiFont(14);
        }
        if (self.normalColor == nil)
        {
            [btn setTitleColor:kLightBlackColor forState:UIControlStateNormal];
        }
        if (self.selectedColor == nil)
        {
            [btn setTitleColor:kBlueColor forState:UIControlStateSelected];
        }
        btn.relayout = YES;
        btn.titleLabelFrame = CGRectMake(btn.width/2-20, 0, btn.width, self.height);
        btn.imageViewFrame = CGRectMake(btn.width/2+10, 17, 11, 6);
        [btn setImage:[UIImage imageNamed:@"arrow_down"] forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:@"arrow_up"] forState:UIControlStateSelected];
        [btn setTitle:_titleArray[i] forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
        btn.tag = 100+i;
        [self addSubview:btn];
        
        UIImageView *menuLine = [[UIImageView alloc] initWithFrame:CGRectMake(btn.right, 10, 0.5, 20)];
        menuLine.image = [UIImage imageNamed:@"dot"];
        [self addSubview:menuLine];
    }
}

- (void)clickBtn:(GXButton *)sender
{
    self.selectedIndex = (int)sender.tag-100;
    for (int i=0; i<_titleArray.count; i++) {
        GXButton *btn = (GXButton *)[self viewWithTag:100+i];
        if (btn.tag!=sender.tag) {
            btn.selected = NO;
        }
    }
}

- (void)setSelectedIndex:(int)value
{
    _selectedIndex = value;
//    for (int i = 0 ; i <_titleArray.count; i++) {
//        GXButton *btn = (GXButton *)[self viewWithTag:100+i];
//        btn.selected = i==value;//  i是否等于value
//        if (btn.selected && self.selectedFont) {
//            btn.titleLabel.font = self.selectedFont;
//        }else {
//            btn.titleLabel.font = self.font;
//        }
//    }

    if ([self.delegate respondsToSelector:@selector(selectImageMenuIndex:)]) {
        [self.delegate selectImageMenuIndex:_selectedIndex];
    }
}

- (void)setBtnStateWithIndex:(NSInteger)index state:(BOOL)state{
    GXButton *btn = (GXButton *)[self viewWithTag:100+index];
    btn.selected = state;
}

- (void)setBtnTitleColorWithIndex:(NSInteger)index state:(BOOL)state{
    GXButton *btn = (GXButton *)[self viewWithTag:100+index];
    if (state) {
        [btn setTitleColor:kBlueColor forState:UIControlStateNormal];
    }
    else{
        [btn setTitleColor:kLightBlackColor forState:UIControlStateNormal];
    }
//    btn.selected = state;
}

@end
